<%@ page import="ar.com.hakasoft.utils.NumberUtil" %>
<%@ page import="ar.com.hakasoft.utils.DateUtil" %>

<html>
<head>
<meta name="layout" content="mainApp" />
<g:javascript library="jquery"  /> 
<g:javascript library="facturacion" />
</head>
<body>
    <g:if test="${flash.message}">
    <div class="message">${flash.message}</div>
    </g:if>
    <g:hasErrors bean="${facturaInstance}">
    <div class="errors">
        <g:renderErrors bean="${facturaInstance}" as="list" />
    </div>
	</g:hasErrors>
	    <g:hasErrors bean="${facturaInstance}">
    <div class="errors">
        <g:renderErrors bean="${facturaInstance}" as="list" />
    </div>
	</g:hasErrors>
 	<g:uploadForm name="form" controller="facturacion" >
 		<g:hiddenField name="id" id="id" value="${facturaInstance.id}" />
 		
	    <div class="dialog" style="margin-left: 200px; border: 1px solid #ccc; width: 500px; align: center;">
	     		
	     	<g:if test="${facturaInstance.id }">
		     	<div class="mensajeAtencion">ATENCI&Oacute;N: &eacute;sta facutra a&uacute;n no fue cobrada</div>
		     	<div style="padding: 5px; text-align: left; padding-left: 20px;">
					<table style="width: 100%; border: 0px;" cellpadding="0" cellspacing="0"><tr>
					<td width="160"><label>N&uacute;mero de factura</label></td>
				 	<td><B>${facturaInstance.id }</B></td>
				 	</tr></table>
		      	</div>
	     	</g:if>
	     		
	      	<div style="padding: 5px; text-align: left; padding-left: 20px;" id="divFecha">
	      		<table style="width: 100%; border: 0px;" cellpadding="0" cellspacing="0"><tr>
	      		<td width="160"><label>Fecha</label></td>
           		<td>
	      			<g:textField id="inputDate" name="fecha" />
	      			<g:hiddenField name="fechaHidden" id="fechaHidden" value="${DateUtil.toString(facturaInstance?.fecha) }" />
           		</td>
           		</tr></table>
	      	</div>
	      	
	      	<div style="padding: 5px; text-align: left; padding-left: 20px;" id="divVendedor">
	      		<table style="width: 100%; border: 0px;" cellpadding="0" cellspacing="0"><tr>
	      		<td width="160"><label>Vendedor</label></td>
           		<td><g:select name="vendedor.id" id="vendedor.id" from="${vendedoresList}" optionKey="id" value="${facturaInstance?.vendedor?.id}" noSelection="['null': '']"  onchange="showDivClientes(this.value)" /> </td>
           		</tr></table>
	      	</div>
	      	
      		<div style="padding: 0px 0; text-align: left; padding-left: 20px;" id="divDietetica">
	      		<table style="width: 100%; border: 0px;" cellpadding="0" cellspacing="0"><tr>
	      		<td width="160"><label>Diet&eacute;tica</label></td>
	           	<td><g:select name="dietetica.id" id="dietetica.id" from="${dieteticasList}" optionKey="id" value="${facturaInstance?.dietetica?.id}" noSelection="['null': '']" onchange="showDivVendedor(this.value)" /></td>
           		</tr></table>
	      	</div>

      		<div style="padding: 0px 0; text-align: left; padding-left: 20px;" id="divCliente">
	      		<table style="width: 100%; border: 0px;" cellpadding="0" cellspacing="0"><tr>
	      		<td width="160"><label>Cliente</label></td>
	           	<td><g:select name="cliente.id" id="cliente.id" from="${clientesList}" optionKey="id" value="${facturaInstance?.cliente?.id}" noSelection="['null': '']" onchange="showDivCliente(this.value)" /></td>
           		</tr></table>
	      	</div>
	      	
			<div style="padding: 0px 0; text-align: left; padding-left: 20px; padding-right: 20px;"><hr /></div>
	      	
	      	<div style="padding: 0px 0; text-align: left; padding-left: 20px; display: none;" id="divClientes">
				<table style="width: 100%; border: 0px;" cellpadding="0" cellspacing="0"><tr>
				<td width="160"><label>Cliente (Vendedor)</label></td>
			 	<td><g:textField name="clienteVendedor" id="clienteVendedor" maxLenght="100" /></td>
			 	</tr></table>
	      	</div>
	      	
      		<div style="padding: 0px 0; text-align: left; padding-left: 20px;">
	      	    <table style="width: 100%; border: 0px;" cellpadding="0" cellspacing="0"><tr>
	      		<td width="160"><label>Articulo</label></td>
	           	<td><g:select name="articulo.id" id="articulo.id" from="${ar.com.hakasoft.entities.Articulo.list()}" optionKey="id" value="${itemFacturaInstance?.articulo?.id}" onChange="cambiaArticulo(true);" />       </td>
           		</tr></table>
	      	</div>
	      	
	      	<div style="padding: 0px 0; text-align: left; padding-left: 20px;">
      			<table style="width: 100%; border: 0px;" cellpadding="0" cellspacing="0"><tr>
	      		<td width="160"><label>Precio Unitario</label></td>
	           	<td width="100px"><g:textField name="precioUnitario" size="10" id="precioUnitario" onkeypress="soloNumeros(event,true)" value="${fieldValue(bean: itemFacturaInstance, field: 'precioUnitario')}" /></td>
	           	<td style="padding-top: 10px;">
	           		<button id="cambiarValor" name="cambiarValor" onclick="cambiaArticulo(true); return false;" >Cambiar valor</button>
	           	</td>
           		</tr></table>
           </div>
	      	
      		<div style="padding: 0px 0; text-align: left; padding-left: 20px;">
                <table style="width: 100%; border: 0px;" cellpadding="0" cellspacing="0"><tr>
	      		<td width="160"><label>Cantidad</label></td>
	           	<td><g:textField name="cantidad" size="5" id="cantidad" value="${fieldValue(bean: itemFacturaInstance, field: 'cantidad')}" onkeypress="soloNumeros(event,null)" /></td>
           		</tr></table>
           </div>
           
           <g:if test="${facturaInstance?.id}">
				<g:if test="${facturaInstance?.dietetica?.id}">
		      		<script type="text/javascript">
			      		showDivVendedor('${facturaInstance?.dietetica?.id}');
			      		document.getElementById('cliente.id').disabled = true;
			      		document.getElementById('vendedor.id').disabled = true;
		      		</script>
		      	</g:if>
		      	<g:if test="${facturaInstance?.vendedor?.id}">
		     	 	<script type="text/javascript">
		      			showDivClientes('${facturaInstance?.vendedor?.id}');
			      		document.getElementById('cliente.id').disabled = true;
			      		document.getElementById('dietetica.id').disabled = true;
		      		</script>
		      	</g:if>
		     	<g:if test="${facturaInstance?.cliente?.id}">
		     	 	<script type="text/javascript">
		     			showDivCliente('${facturaInstance?.cliente?.id}');
			      		document.getElementById('vendedor.id').disabled = true;
			      		document.getElementById('dietetica.id').disabled = true;
		      		</script>
		      	</g:if>
		      	
	      	</g:if>
	    </div>
	    <br><br>
	    <div align="center">
	    
		    	<g:submitToRemote name="guardar" id="guardar" before="enableInput('precioUnitario'); if(!validar()) return false" 
		    	after="disableInput('precioUnitario'); limpiarTextFields();disableInput('vendedor.id'); disableInput('dietetica.id'); focusElement(); " 
		    	name="Guardar" value="Guardar" onLoading="showSpinner('spinner', true);" onComplete="showSpinner('spinner', false); puedeFinalizar();"  
		    	url="[controller:'facturacion',action:'save']" update="items" />
		    	
<%--		    	<g:if test="${!facturaInstance.id }">--%>
<%--			    	<g:submitToRemote id="nuevo" before="if(!confirm('Desea crear una nueva factura?')) return false" --%>
<%--			    	after="limpiarCombos();limpiarTextFields();new Ajax.Updater('divClientes','/Dietetica/facturacion/mostrarClientes',{asynchronous:true,evalScripts:true,parameters:'vendedor='+this.value});return false;" --%>
<%--			    	name="NuevaFactura" value="Nuevo" --%>
<%--			    	url="[controller:'facturacion',action:'nuevaFactura']" update="items" />--%>
<%--		    	</g:if>--%>
		    	
		    	<g:actionSubmit name="finalizarFactura" id="finalizarFactura" onclick="if(!confirm('Desea finalizar la factura?')) return false"
		    	 value="Finalizar" action="finalizar"  />
		    	 
		    	<g:actionSubmit name="finalizarSinCobro" id="finalizarSinCobro" onclick="if(!confirm('Desea finalizar la factura pero sin haberla cobrado? ATENCION: Esta factura aparecera en otro listado y podra ser aprobada en el mismo')) return false"
		    	 value="Finalizar Sin Cobro" action="finalizarSinCobro"  />
	    </div>
	    <br><br>
	    
	    <div id="items">
	    	<g:render template="/templates/tablaItems" model="['edit':true, 'listadoItems': facturaInstance?.itemFactura]"></g:render>
	    </div>
	    <script type="text/javascript">
			document.getElementById('nombreSeccion').innerHTML = "Facturación";
			puedeFinalizar();
	</script>
	</g:uploadForm>

</body>
</html>